package com.fau.cm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalOrdersProduct;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 订单产品Action
 * @author kangyj
 * @datetime 2014年11月23日,下午10:44:44
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class OrdersProductAction extends PublicAction implements ModelDriven<MedicalOrdersProduct> {

	private static final long serialVersionUID = -1892719346239042272L;
	
	private MedicalOrdersProduct ordersProduct = new MedicalOrdersProduct();

	/**
	 * @desc 打开选择订单产品
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月23日,下午4:45:04
	 */
	@Action(value = "orders_product_select", results = {@Result(location = "orders_product_select.jsp")})
	public String selectMedAndPre() {
		return SUCCESS;
	}
	
	/**
	 * @desc 查询产品/赠品数据
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月23日,下午4:45:57
	 */
	@Action(value = "orders_product_select_view")
	public String selectView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ordersProduct.setHospitalId(admin.getEnterpriseId());
		pageBean = this.baseAllService.getOrdersManagerService().findMedAndPreByPage(ordersProduct, map);
		map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 根据订单id查询产品列表
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月23日,下午11:20:17
	 */
	@Action(value = "orders_product_view")
	public String view() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getOrdersManagerService().findOrdersProductList(ordersProduct.getOrdersId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 保存订单产品
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月23日,下午10:45:01
	 */
	@Action(value = "orders_product_save")
	public String save() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(ordersProduct.getOrdersId())) {
			this.baseAllService.getOrdersManagerService().saveOrdersProduct(ordersProduct);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除订单产品
	 * @return
	 * @author kangyj
	 * @datetime 2014年11月24日,上午12:01:16
	 */
	@Action(value = "orders_product_del")
	public String del() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(ordersProduct.getId())) {
			this.baseAllService.getOrdersManagerService().delOrdersProduct(ordersProduct);
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 验证所选药品是否已存在。
	 * @return
	 * @author kangyj
	 * @datetime 2015年3月11日,上午12:57:01
	 */
	@Action(value = "orders_product_valid_exist")
	public String validProExist() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getOrdersManagerService().validProExist(ordersProduct);
		map.put("flag", flag);
		formatJSON(response, map);
		return null;
	}
	
	
	@Override
	public MedicalOrdersProduct getModel() {
		return ordersProduct;
	}

}
